/*
Illuminator (LED drawing tool for iPhone)
Copyright (C) 2008
Nik S Denin
Ivan A Vlasov
Dmitriy A Jackovlev

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#import "ILTopToolbar.h"
#import "DrawField.h" // for delegate methods

#define ICON_WIDTH 26.0
#define ICON_HEIGHT 40.0

@implementation ILTopToolbar

- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)image
{
	if ((self = [super initWithFrame:frame withImage:image]) != nil)
	{
		float x = 10.0;
		float y = (frame.size.height - ICON_HEIGHT) / 2.0;

		m_smallFieldButt = [[UIPushButton alloc] initWithFrame:CGRectMake(x, y, ICON_WIDTH, ICON_HEIGHT)];
		[m_smallFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size1_0.png"] forState:0];
		[m_smallFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size1_1.png"] forState:1];
		[m_smallFieldButt addTarget:self action:@selector(setSmallField) forEvents:UIMouseDown];
		x += ICON_WIDTH;

		m_mediumFieldButt = [[UIPushButton alloc] initWithFrame:CGRectMake(x, y, ICON_WIDTH, ICON_HEIGHT)];
		[m_mediumFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size2_1.png"] forState:0];
		[m_mediumFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size2_0.png"] forState:1];
		[m_mediumFieldButt addTarget:self action:@selector(setMediumField) forEvents:UIMouseDown];
		x += ICON_WIDTH;

		m_largeFieldButt = [[UIPushButton alloc] initWithFrame:CGRectMake(x, y, ICON_WIDTH, ICON_HEIGHT)];
		[m_largeFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size3_0.png"] forState:0];
		[m_largeFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size3_1.png"] forState:1];
		[m_largeFieldButt addTarget:self action:@selector(setLargeField) forEvents:UIMouseDown];
		x += (3.0 * ICON_WIDTH);

		m_invertImageButt = [[UIPushButton alloc] initWithFrame:CGRectMake(x, y, ICON_WIDTH, ICON_HEIGHT)];
		[m_invertImageButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_invert_0.png"] forState:0];
		[m_invertImageButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_invert_1.png"] forState:1];
		[m_invertImageButt addTarget:m_delegate action:@selector(invertImage) forEvents:UIMouseDown];
		x += ICON_WIDTH;

		m_fillButt = [[UIPushButton alloc] initWithFrame:CGRectMake(x, y, ICON_WIDTH, ICON_HEIGHT)];
		[m_fillButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_fill_0.png"] forState:1];
		[m_fillButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_fill_1.png"] forState:0];
		[m_fillButt addTarget:self action:@selector(swapFill) forEvents:UIMouseDown];
		x += (3.0 * ICON_WIDTH);

		m_undoButt = [[UIPushButton alloc] initWithFrame:CGRectMake(x, y, ICON_WIDTH, ICON_HEIGHT)];
		[m_undoButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_undo_0.png"] forState:0];
		[m_undoButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_undo_1.png"] forState:1];
		[m_undoButt addTarget:m_delegate action:@selector(undo) forEvents:UIMouseDown];
		x += ICON_WIDTH;

		m_redoButt = [[UIPushButton alloc] initWithFrame:CGRectMake(x, y, ICON_WIDTH, ICON_HEIGHT)];
		[m_redoButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_redo_0.png"] forState:0];
		[m_redoButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_redo_1.png"] forState:1];
		[m_redoButt addTarget:m_delegate action:@selector(redo) forEvents:UIMouseDown];

		[self addSubview:m_smallFieldButt];
		[self addSubview:m_mediumFieldButt];
		[self addSubview:m_largeFieldButt];
		[self addSubview:m_invertImageButt];
		[self addSubview:m_fillButt];
		[self addSubview:m_undoButt];
		[self addSubview:m_redoButt];
	}
	
	return self;
}

- (void)setSmallField
{
	// turn on smallFieldButt
	[m_smallFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size1_0.png"] forState:1];
	[m_smallFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size1_1.png"] forState:0];

	// turn off others
	[m_mediumFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size2_0.png"] forState:0];
	[m_mediumFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size2_1.png"] forState:1];

	[m_largeFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size3_0.png"] forState:0];
	[m_largeFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size3_1.png"] forState:1];

	[m_delegate setSmallField];
}

- (void)setMediumField
{
	// turn on mediumFieldButt
	[m_mediumFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size2_0.png"] forState:1];
	[m_mediumFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size2_1.png"] forState:0];

	// turn off others
	[m_smallFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size1_0.png"] forState:0];
	[m_smallFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size1_1.png"] forState:1];

	[m_largeFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size3_0.png"] forState:0];
	[m_largeFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size3_1.png"] forState:1];

	[m_delegate setMediumField];
}

- (void)setLargeField
{
	// turn on largeFieldButt
	[m_largeFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size3_0.png"] forState:1];
	[m_largeFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size3_1.png"] forState:0];

	// turn off others
	[m_smallFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size1_0.png"] forState:0];
	[m_smallFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size1_1.png"] forState:1];

	[m_mediumFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size2_0.png"] forState:0];
	[m_mediumFieldButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_size2_1.png"] forState:1];

	[m_delegate setLargeField];
}

- (void)swapFill
{
	switch([m_delegate getFillingState])
	{
		case DFFilling:
		{
			[m_fillButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_fill_0.png"] forState:1];
			[m_fillButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_fill_1.png"] forState:0];
			[m_delegate setFill:DFErasing];
			break;
		}
		case DFErasing:
		{
			[m_fillButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_fill_0.png"] forState:0];
			[m_fillButt setImage:[UIImage applicationImageNamed:@"toolbar_icons/icon_fill_1.png"] forState:1];
			[m_delegate setFill:DFFilling];
			break;
		}
		default: break;
	}
}

- (void)dealloc
{
	[m_smallFieldButt release];
	[m_mediumFieldButt release];
	[m_largeFieldButt release];

	[m_invertImageButt release];
	[m_fillButt release];

	[m_undoButt release];
	[m_redoButt release];

	[super dealloc];
}

@end
